package intranet.controllers;
import org.springframework.web.servlet.ModelAndView;
import intranet.models.*;
import intranet.tools.*;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	/* IsNumber Function */
	public static boolean IsNumber(String num) {
        try {
            Integer.parseInt(num);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
	
	/* -- NOTICE BOARDS -- */

	
	
	
	

	
	
	
	
	/* -- FILE SHARING -- */
	
	/*@RequestMapping("/myuploads.html")
	public ModelAndView Myuploads(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String page= request.getParameter("page");
		MyuploadModel Myupload=new MyuploadModel();
		ResultSet rs=Myupload.SelectMyupload(1);
		ArrayList Myuploads=this.resultSetToArrayList(rs);
		int RowCount=Myuploads.size();
		ModelAndView mv=new ModelAndView("index");
		//Passing Page Included
		mv.addObject("includethispage", "myuploads.jsp");
		// Passing Data Record
		mv.addObject("data", Myuploads);
		page=(page==null || IsNumber(page)==false)?"1":page;
		// Pagination Link Elements
		int PageSize=3;
		int PageCount=Math.round(RowCount/PageSize);
		page=(Integer.parseInt(page)<=0 ||Integer.parseInt(page)>PageCount)?"1":page;
		int StartingRow=(Integer.parseInt(page)-1)*PageSize;
		int EndingRow=(Integer.parseInt(page)*PageSize)-1;
		mv.addObject("page", page);
		mv.addObject("PageCount", PageCount);
		mv.addObject("StartingRow", StartingRow);
		mv.addObject("EndingRow", EndingRow);
		return mv;
	}*/
	
	/*@RequestMapping("/edit_myupload.html")
	public ModelAndView EditMyuploads(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
		String id= request.getParameter("id");
		id=(id==null || IsNumber(id)==false)?"0":id;
		id=(Integer.parseInt(id)<=0)?"0":id;
		// IF Requested ID=0
		if(Integer.parseInt(id)==0){
			return this.Myuploads(request, response);
		}else{
			MyuploadModel Myupload=new MyuploadModel();
			FileCategoryModel CategoryModel=new FileCategoryModel();
			ResultSet rs=Myupload.SelectUploadedFile(Integer.parseInt(id));
			ResultSet rscat=CategoryModel.SelectFileCategory(0);
			ArrayList Myuploads=this.resultSetToArrayList(rs);
			ArrayList FileCategories=this.resultSetToArrayList(rscat);
			ModelAndView mv=new ModelAndView("index");
			//Passing Page Included
			mv.addObject("includethispage", "edit_myupload.jsp");
			// Passing Data Record & Image_File
			mv.addObject("data", Myuploads);
			// Passing ALL FILE CATEGORIES
			mv.addObject("categories", FileCategories);
			return mv;
		}	
	}*/
	
	/*@RequestMapping("/update_myuploads.html")
	public void UpdateMyuploads(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{
		String id= request.getParameter("txtid");
		String title=request.getParameter("txttitle");
		String cat_id=request.getParameter("txtcat");
		String short_desc=request.getParameter("txtshortdesc");
		String full_desc=request.getParameter("txtfulldesc");
		MyuploadModel Myuploads=new MyuploadModel();
		Myuploads.UpdateMyupload(Integer.parseInt(id),title,Integer.parseInt(cat_id),short_desc,full_desc);
		response.sendRedirect("myuploads.html");
		
	}*/

	
	
	
	// RESULTSET TO ARRAYLIST
	   public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{
		    ResultSetMetaData md = rs.getMetaData();
		    int columns = md.getColumnCount();
		    ArrayList results = new ArrayList();
		    while (rs.next()){
		    HashMap row = new HashMap();
		    results.add(row);
		    for(int i=1; i<=columns; i++){
		    row.put(md.getColumnName(i),rs.getObject(i));
		    }
		    }
		    return results;

	    }
}
